
import { Injectable } from '@angular/core';
import { Router, Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ForumService } from './ForumService';
import { Forum } from '../models/Forum';

@Injectable()
export class ForumsResolveService implements Resolve<{total: number, forums: Forum[]}> {

  constructor(private forumService: ForumService) {}

  resolve(route: ActivatedRouteSnapshot): Observable<{total: number, forums: Forum[]}>  {

    return this.forumService.getForums().pipe(map(res => {
      if (res.success) {
        return  {total: res.total, forums: res.res};
      } else {
        console.error(res.res);
        return {total: 0, forums: []};
      }
    }));
  }
}

